package com.lx.ngsl.core.role.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lx.ngsl.core.role.entity.RoleEntity;
import com.lx.ngsl.core.role.service.RoleMenuService;
import com.lx.ngsl.core.role.service.RoleService;
/**
 * 角色控制类
 * @ClassName:：RoleController 
 */
@RestController
@RequestMapping("/v0")
public class RoleController {

	@Autowired
	private RoleMenuService roleMenuService;
	@Autowired
	private RoleService service;
	
	/**
	 * 
	 * <p>Method ：queryRoles
	 * <p>Description :角色分页查询方法 
	 * 
	 *
	 * @param name
	 * @param description
	 * @param pageable
	 * @return
	 * @throws Exception 
	 *</p>
	 */
	@GetMapping("/roles")
	public Page<RoleEntity> queryRoles(
			@RequestParam(value = "name", required = false, defaultValue = "") String name, 
			@RequestParam(value = "description", required = false, defaultValue = "") String description, 
			Pageable pageable
			) throws Exception{
		
		return service.roleListPage(name, description, pageable);
	}
	
	/*
	 * 新增角色
	 */
	@PostMapping("/role")
	public boolean addRole(@RequestBody RoleEntity role) {
		return service.saveRole(role);
	}
	
	/*
	 * 角色修改
	 */
	@PutMapping("/role/{id}")
	public void editRole(@PathVariable(value="id") String id,@RequestBody RoleEntity role) throws Exception {
		if(service.existRole(id)) {
			service.saveRole(role);
		}else {
			throw new Exception("该角色不存在！");
		}
	}
	
	
	@RequestMapping(value = "/role/init", method = RequestMethod.GET)
	public void init() {
		roleMenuService.init();
	}
	
	/**
	 * 查询所有角色id和角色名称的对应关系
	 * @Title：roleMap 
	 * @return 
	 * @return ：Map<String,String> 
	 * @throws
	 */
	@RequestMapping(value = "/role/roleMap", method = RequestMethod.GET)
	private Map<String,String> roleMap(){
		return service.roleMap();
	}
	
	/**
	 * 查询所有角色信息
	 * @Title：roleDropdownTree 
	 * @return
	 * @throws Exception 
	 * @return ：List<RoleEntity> 
	 * @throws
	 */
	@RequestMapping(value = "/role/dropdowntree", method = RequestMethod.GET)
	public List<RoleEntity> roleDropdownTree() throws Exception {
		List<RoleEntity> list = service.roleList();
		return list;
	}
	
	
}
